discard

/** get data **/
	set more off 
	use "$healthsave\with medication_w7.dta", clear 

	
/** drop unnecessary variables **/
		
	drop hediaw* hedim* hedias* heag* hedib* hediad* ///
			hedan* hedac* hedaw* hedbs* 
			
			
	** we have now derived internally consistent variables for 'ever diagnosed' 
	** (consistent within waves) 
	
	** some people are fed forward as having a condition, despite never having 
	** reported it. They then dispute the condition, CORRECTLY. 
	** This is a survey error and we don't want to capture it in our dispute 
	** variable. 
	
	tokenize LungDisease Asthma Arthritis Osteoporosis Cancer Parkinsons ///
			 PsychiatricProblems Alzheimers Dementia BloodDisorder 		 ///
			 Hypertension Angina HeartAttack HeartFailure HeartMurmur	 ///
			 HeartRhythm Diabetes Stroke Cholesterol	
		
	foreach x in lu as ar os ca pd ps ad de bl bp an mi hf hm hr di st ch { 
	
	
	/* number of reports */	
	
		gen nrep`x'_w0 = (ever`x'_w0==1) 
		lab var nrep`x'_w0 "Number of reports of `1' up to and including wave 0"

		forv i = 1/7 { 
		
			local Pi = `i'-1 
			gen nrep`x'_w`i' = .
			lab var nrep`x'_w`i' "Number of reports of `1' up to and including wave `i'"
			replace nrep`x'_w`i' = (ever`x'_w`i'==1) + nrep`x'_w`Pi' 
			
		}
		mac shift 	
	}
	
	/** how many people are ff despite no previous reports of a condition? */
	
	tokenize LungDisease Asthma Arthritis Osteoporosis Cancer Parkinsons ///
			 PsychiatricProblems Alzheimers Dementia BloodDisorder 		 ///
			 Hypertension Angina HeartAttack HeartFailure HeartMurmur	 ///
			 HeartRhythm Diabetes Stroke Cholesterol		
	foreach x in lu as ar os ca pd ps ad de bl bp an mi hf hm hr di st ch { 
		forv i = 1/7 {
			local Pi = `i'-1 
		
		qui:  count if nrep`x'_w`Pi' == 0 & ff`x'_w`i' == 1 
		 if `r(N)' >0 { 
			di in red "`r(N)' instances of ff error for `1' in wave `i'" 
		 }
		}
		mac shift 
	}
	
		/** for documentation **/	
			cd "P:\ELSA\Healthdynamics\documentation\tables\" 		
			
			preserve 
			foreach x in lu as ar os ca pd ps ad de bl bp an mi hf hm hr di st ch { 
				forv i = 1/7 {
				local Pi = `i'-1 
		
				gen fferror`x'`i' = nrep`x'_w`Pi' == 0 & ff`x'_w`i' == 1 			
				}
				local foreshape `foreshape' fferror`x' 
				local fortabout `fortabout' sum fferror`x' 
			}
			
			keep idauniq inwave* fferror* 
			drop inwave0 inwave
			reshape long `foreshape' inwave, i(idauniq) j(wave) 
			keep if inwave==1 
			tabout wave using total_fferrors_w7.txt , ///
				c(`fortabout') f(4) sum replace 
			restore 
	
	
	
	** for those who then dispute these bogus ff, no problem.
	** keep an indicator of whether this has been changed and change 
	** their ff indicator to NO, their dispute indicator to n/a 
	
	foreach x in lu as ar os ca pd ps ad de bl bp an mi hf hm hr di st ch { 
	forv i = 2/7 { 
		local Pi = `i' -1 	
		di in red "`x' `i' "
		gen badffdisp`x'_w`i' = nrep`x'_w`Pi' == 0 & ff`x'_w`i' == 1 ///
				& disp`x'_w`i' >= 3 & disp`x'_w`i'<=5 				
		lab var badffdisp`x'_w`i' "Bogus ff to wave `i' correctly disputed" 		
		}
	}
		
	preserve 
	keep idauniq badffdisp* 
	save "$healthsave\record of disputed bad ffs_w7.dta", replace 
	restore 
	
	
	/** for documentation **/	
			
			cd "P:\ELSA\Healthdynamics\documentation\tables\" 		
			
			preserve 
			
			foreach x in lu as ar os ca pd ps ad de bl bp an mi hf hm hr di st ch { 
				forv i = 1/7 {
				local Pi = `i'-1 
				gen fferror`x'`i' = nrep`x'_w`Pi' == 0 & ff`x'_w`i' == 1 			
				}
			}
						
			local fortabout 
			foreach x in lu as ar os ca pd ps ad de bl bp an mi hf hm hr di st ch { 
				egen totbadffdisp`x' = anymatch(badffdisp`x'_w?) , v(1) 
				local fortabout `fortabout' sum totbadffdisp`x' 
			}
			
			local fortabout2 
			foreach x in lu as ar os ca pd ps ad de bl bp an mi hf hm hr di st ch { 
				egen totfferror`x' = anymatch(fferror`x'?) , v(1) 
				local fortabout2 `fortabout2' sum totfferror`x' 
			}			
						
			gen n = 1 
			tabout n using people_fferrors.txt , c(`fortabout2') sum replace 		
			tabout n using people_fferrors_immediately_disputed_w7.txt , c(`fortabout') sum replace 
			
			
			
			local fortabout 
			local foreshape 
			ren badffdisp??_w?  badffdisp???
			foreach x in lu as ar os ca pd ps ad de bl bp an mi hf hm hr di st ch { 
				local foreshape `foreshape' badffdisp`x'
				local fortabout `fortabout' sum badffdisp`x'
			}
			
			keep idauniq inwave* badffdisp* 
			drop inwave0 inwave
			reshape long `foreshape' inwave, i(idauniq) j(wave) 
			keep if inwave==1 
			tabout wave using fferrors_immediately_disputed_w7.txt , ///
				c(`fortabout') f(4) sum replace 
						
			restore 	
			

	foreach x in lu as ar os ca pd ps ad de bl bp an mi hf hm hr di st ch { 
		forv i = 2/7 { 	
	
		replace ff`x'_w`i' = 0 if badffdisp`x'_w`i' == 1 
		replace disp`x'_w`i' = 1 if badffdisp`x'_w`i' == 1 
		
		}
	}
		
	
	drop badffdisp* 
	
		
		
	/** NOW derive dispute indicators **/
	
	** (i)  derive variables that we will use later, indicating how many 
	** 		times the respondent has effectively reported the condition 
	
	** (ii) check for consistency across waves 
	
	tokenize LungDisease Asthma Arthritis Osteoporosis Cancer Parkinsons ///
			 PsychiatricProblems Alzheimers Dementia BloodDisorder 		 ///
			 Hypertension Angina HeartAttack HeartFailure HeartMurmur	 ///
			 HeartRhythm Diabetes Stroke Cholesterol	
	
	foreach x in lu as ar os ca pd ps ad de bl bp an mi hf hm hr di st ch { 
	
	** dispute indicator 
	
		egen anydisp`x' = anymatch(disp`x'_w?) , v(3 4 5) 
		lab var anydisp`x' "Ever disputes previous report of `1'" 

	** total number of disputes 
		
		gen totdisp`x' = (disp`x'_w2 >=3 & disp`x'_w2<=5) ///
						+ (disp`x'_w3 >=3 & disp`x'_w3<=5) ///
						+ (disp`x'_w4 >=3 & disp`x'_w4<=5) ///
						+ (disp`x'_w5 >=3 & disp`x'_w5<=5) ///
						+ (disp`x'_w6 >=3 & disp`x'_w6<=5) ///
						+ (disp`x'_w7 >=3 & disp`x'_w7<=5)
						
		lab var totdisp`x' "Total number of disputes of `1'" 		
	
	
		mac shift 
	}
	
 save "$healthsave\with problem indicators_w7.dta", replace 
